package 爆刷leetcode.balabala;

import java.security.Key;

/**
 * @Author yaya
 * @Create 2024/5/24 8:26
 * Description:盛水最多的容器
 */

public class maxArea {

    public static void main(String[] args) {
        int height[] = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
        int area = 0;
        area = maxArea(height);
        System.out.println(area);

        area = maxAreaByFor(height);
        System.out.println(area);
    }


    // 双循环
    public static int maxAreaByFor(int[] height) {

        int area = 0;

        for (int i = 0; i < height.length; i++) {
            for (int j = i + 1; j < height.length; j++) {
                int high = min(height[i], height[j]);
                int width = j - i;
                area = max(area, high * width);
            }
        }

        return area;
    }

    // 左右指针
    public static int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int area = 0;
        int high = 0;
        int width = 0;
        while (left < right) {
            high = min(height[left], height[right]);;
            width = right - left;
            area = max(area, high * width);

            if (height[left] == high && left < right){
                while (height[left] <= high) {
                    if (left >= right) {
                        break;
                    }
                    left ++;
                }
            }

            if (height[right] == high && left < right) {
                while (height[right] <= high) {
                    if (left >= right) {
                        break;
                    }
                    right --;
                }
            }

        }
        return area;
    }

    public static int min(int num1, int num2) {
        if (num1 <= num2) return num1;
        return num2;
    }

    public static int max(int num1, int num2) {
        if (num1 >= num2) return num1;
        return num2;
    }
}
